package com.cz.run.beans.base;

import com.cz.run.enums.SystemCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author CAIYJ
 * @Description: TODO()
 * @Date:2018/5/15 15:16
 */
@ApiModel
public class ReturnDTO<T> {
    /**
     * @Fields code : 返回码
     */
    @ApiModelProperty(value = "状态码 200表示成功；其他表示失败", example = "状态码 200表示成功；其他表示失败")
    private Integer status;

    /**
     * @Fields error : 返回异常信息
     */
    @ApiModelProperty(value = "错误信息", example = "错误信息")
    private String error;

    /**
     * @Fields message :返回信息
     */
    @ApiModelProperty(value = "返回提示信息", example = "返回提示信息")
    private String message;

    /**
     * @Fields data : 返回结果集
     */
    @ApiModelProperty(value = "数据", example = "")
    private T data;

    public ReturnDTO() {
        super();
    }

    public ReturnDTO(Integer status, String error, String message, T data) {
        super();
        this.status = status;
        this.error = error;
        this.message = message;
        this.data = data;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> ReturnDTO<T> success(T data) {
        return new ReturnDTO<T>(SystemCodeEnum.getCode("SUCCESS"), null, null,
                data);
    }

    @SuppressWarnings("unchecked")
    public static <T> ReturnDTO<T> fail(SystemCodeEnum systemCodeEnum) {
        return new ReturnDTO<T>(systemCodeEnum.getErrorCode(),
                systemCodeEnum.getErrorMsg(), systemCodeEnum.getErrorMsg(),
                (T) systemCodeEnum.getErrorMsg());
    }

    @SuppressWarnings("unchecked")
    public static <T> ReturnDTO<T> fail(String message) {
        return new ReturnDTO<T>(SystemCodeEnum.error_service.getErrorCode(),
                message, message,
                (T) message);
    }

    @SuppressWarnings("unchecked")
    public static <T> ReturnDTO<T> error(String message) {
        return new ReturnDTO<T>(SystemCodeEnum.error_invalid_argument.getErrorCode(),
                message, message,
                (T) message);
    }

    @Override
    public String toString() {
        return "ReturnDTO{" +
                "status=" + status +
                ", error='" + error + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
